package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONConverter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.controller.dto.ReserveDTO;
import com.wy.entity.Diagnosis;
import com.wy.entity.Pet;
import com.wy.service.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.entity.Reserve;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-29
 */
@Slf4j
@RestController
@RequestMapping("/reserve")
public class ReserveController {

    @Resource
    private IReserveService reserveService;

    @Resource
    private IPetService petService;

    @Resource
    private ICustomerService customerService;

    @Resource
    private IUserService userService;

    @Resource
    private IPetTypeService petTypeService;

    /****
     * 根据用户id获取预约信息列表
     * @param customerId
     * @return
     */
    @GetMapping("/{customerId}")
    public R getCustomerReserveList(@PathVariable Integer customerId) {
        List<Reserve> list = reserveService.list(new LambdaQueryWrapper<Reserve>().eq(Reserve::getCustomerId, customerId));
        list.stream().forEach(item -> {
            item.setPetName(petService.getById(item.getPetId()).getName());
            item.setWeight(petService.getById(item.getPetId()).getWeight());
            item.setDoctorName(userService.getById(item.getUserId()).getNickname());
        });
        return R.success(list);
    }

    /****
     *后台预约管理获取预约信息分页列表
     */
    @GetMapping("/getReservePage")
    public R getgetReservePage(@RequestParam Integer userId,
                               @RequestParam Integer page,
                               @RequestParam Integer pageSize,
                               @RequestParam String name) {
        String role = userService.getById(userId).getRole();
        LambdaQueryWrapper<Reserve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Reserve::getUpdateTime);
        if (role.equals("ROLE_DOCTOR")) {
            queryWrapper.eq(Reserve::getUserId, userId);
        }
        Page<Reserve> page1 = reserveService.page(new Page<>(page, pageSize), queryWrapper);
        page1.getRecords().forEach(item -> {
            Pet pet = petService.getById(item.getPetId());
            item.setImage(pet.getImage());
            item.setPetName(pet.getName());
            item.setAge(pet.getAge());
            item.setTypeName(petService.getById(item.getPetId()).getTypeName());
            item.setWeight(petService.getById(item.getPetId()).getWeight());
            item.setDoctorName(userService.getById(item.getUserId()).getNickname());
            item.setCustomerName(customerService.
                    getById(item.getCustomerId()).getName());
            item.setImage(pet.getImage());
        });
        List<Reserve> collect = page1.getRecords().stream().
                filter(item -> item.getPetName().contains(name)).collect(Collectors.toList());
        page1.setRecords(collect);
        return R.success(page1);
    }


    // 新增或者更新
    @PostMapping
    public R save(@RequestBody Reserve reserve) {
        reserveService.saveOrUpdate(reserve);
        return R.success("");
    }
}

